import { createRouter, createWebHistory } from 'vue-router'
import { useUserStore } from '@/stores/index.js'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/404',
      name: '404',
      component: () => import('@/views/error/404.vue')
    },
    {
      path: '/:pathMatch(.*)',
      name: 'notFound',
      hidden: true,
      redirect: '/404'
    },
    {
      path: '/login',
      name: 'Login',
      component: () => import('@/views/login/index.vue')
    },
    {
      path: '/',
      redirect: '/song/manage',
      component: () => import('@/views/layout/index.vue'),
      children: [
        {
          path: '/song/manage',
          name: 'Song',
          component: () => import('@/views/layout/song/index.vue')
        },
        {
          path: '/playlist/manage',
          name: 'Playlist',
          component: () => import('@/views/layout/playlist/index.vue')
        },
        {
          path: '/album/manage',
          name: 'Album',
          component: () => import('@/views/layout/album/index.vue')
        },
        {
          path: '/comment',
          name: 'Comment',
          component: () => import('@/views/layout/comment/index.vue')
        },
        {
          path: '/type',
          name: 'Type',
          component: () => import('@/views/layout/type/index.vue')
        },
        {
          path: '/user/info',
          name: 'UserInfo',
          component: () => import('@/views/layout/user/UserInfo.vue')
        },
        {
          path: '/user/manage',
          name: 'UserManage',
          component: () => import('@/views/layout/user/index.vue')
        }
      ]
    },
    {
      path: '/home',
      name: 'Home',
      redirect: '/home/main',
      component: () => import('@/views/home/index.vue'),
      children: [
        {
          path: '/home/main',
          name: 'Main',
          component: () => import('@/views/home/main.vue')
        },
        {
          path: '/home/album',
          name: 'HomeAlbum',
          component: () => import('@/views/home/album.vue')
        },
        {
          path: '/home/playlist',
          name: 'HomePlaylist',
          component: () => import('@/views/home/Playlist.vue')
        },
        {
          path: '/home/song/detail',
          name: 'SongDetail',
          component: () => import('@/views/home/SongDetail.vue')
        },
        {
          path: '/home/song/list',
          name: 'SongList',
          component: () => import('@/views/home/SongList.vue')
        },
        {
          path: '/home/user/follow',
          name: 'UserFollow',
          component: () => import('@/views/home/UserFollow.vue')
        }
      ]
    }
  ]
})
const antUrls = [
  '/song/manage',
  '/playlist/manage',
  '/album/manage',
  '/comment',
  '/type',
  '/user/info',
  '/user/manage'
]
// const antUrls = []
router.beforeEach(async (to) => {
  const userStore = useUserStore()
  // 访问需要登录的页面时
  if (antUrls.includes(to.path) && !userStore.user.token) {
    // 没有token直接跳转登录
    return '/login'
  }
  // 无需登录的地方直接放行
  return true
})

export default router
